package com.google.unizone.server;

import java.util.LinkedList;
import java.lang.Object;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.jdo.annotations.Embedded;
import javax.jdo.annotations.FetchGroup;
import javax.jdo.annotations.PersistenceCapable;  
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(detachable = "true")
@FetchGroup(name = "lessons", members = { @Persistent(name =  "lessonTimes") })  

public class Course{

	@PrimaryKey
	@Persistent
	private String courseID;
	@Persistent
	private String courseName;
	
	@Persistent(defaultFetchGroup="true")
	//@Embedded
	private List<Lesson> lessonTimes;
	
	
	//@Persistent
	//private List<String> lessonKeys;
	
	
	
	@Persistent
	private int day1, month1, year1;
	@Persistent
	private int day2, month2, year2;
	
	@Persistent(defaultFetchGroup="true")
	@Embedded
	private MyTime hour1, hour2;	
	
	@Persistent
	private String semester;
	
	@Persistent
	private Set<String> studentIDList;
	
	@Persistent
	private String courseType;
	
	public Course()
	{
		this.lessonTimes = new LinkedList<Lesson>();
		this.studentIDList = new TreeSet<String>();
	}
	
	//TODO add lesson Times parsing
		public Course(String courseID, String courseName, List<Lesson> lessonTimes, MyDate moedADate, MyDate moedBDate, String semester, Set<String> studentIDList){
			this.courseID = courseID;
			this.courseName = courseName;
			this.lessonTimes = new LinkedList<Lesson>(lessonTimes);
			
			this.day1=moedADate.getDay();
			this.month1=moedADate.getMonth();
			this.year1=moedADate.getYear();
			this.hour1=moedADate.getHour();
			
			this.day2=moedBDate.getDay();
			this.month2=moedBDate.getMonth();
			this.year2=moedBDate.getYear();
			this.hour2=moedBDate.getHour();
			
			this.semester = semester;
			this.studentIDList = new TreeSet<String>(studentIDList);
		}
		
		
		public Set<String> getStudentIDList() {
			return this.studentIDList;
		}


		public void setStudentIDList(Set<String> studentIDList) {
			this.studentIDList = studentIDList;
		}



		public String getID() {
			return this.courseID;
		}
		public String getCourseName() {
			return this.courseName;
		}
		public String getCourseType() {
			return this.courseType;
		}
		public List<Lesson> getLessonTimes() {
			return this.lessonTimes;
		}
		public MyDate getMoedADate() {
			return new MyDate(this.day1, this.month1, this.year1, this.hour1);
		}
		
		public MyDate getMoedBDate() {
			return new MyDate(this.day2, this.month2, this.year2, this.hour2);
		}
		
		public String getSemester() {
			return this.semester;
		}
		public Set<String> getStudents() {
			return this.studentIDList;
		}
		
		//SETTERS
		public void setCourseType(String courseType) {
			this.courseType = courseType;
		}
		
		public void setCourseName(String courseName) {
			this.courseName = courseName;
		}
		
		public void setLessonTimes(List<Lesson> lessonTimes) {
			this.lessonTimes = lessonTimes;
		}
		//public void setMoedADate(FORMAT??);
		//public void setMoedBDate(FORMAT??);
		public void setID(String courseID) {
			this.courseID  = courseID; 
		}
		public void setSemester(String semester) {
			this.semester = semester;
		}
		
		public void setMoedADate(MyDate date) {
			this.day1=date.getDay();
			this.month1=date.getMonth();
			this.year1=date.getYear();
			this.hour1=date.getHour();
		}
		
		public void setMoedBDate(MyDate date) {
			this.day2=date.getDay();
			this.month2=date.getMonth();
			this.year2=date.getYear();
			this.hour2=date.getHour();
		}
		
		public void addStudent(String id) {
			if (!isStudentInCourse(id)) {
				this.studentIDList.add(id);
			}
		}
		public void removeStudent(String id) {
			if (isStudentInCourse(id)) {
				this.studentIDList.remove(id);
			}
		}
		public void addStudent(Student stu) {
			if (!isStudentInCourse(stu)) {
				this.studentIDList.add(stu.getFacebookID());
			}
		}
		public void removeStudent(Student stu) {
			if (!isStudentInCourse(stu)) {
				this.studentIDList.remove(stu.getFacebookID());
			}
		}
		public boolean isStudentInCourse(String studentID) {
			return this.studentIDList.contains(studentID);
		}
		public boolean isStudentInCourse(Student student) {
			return isStudentInCourse(student.getFacebookID());
		}
		
		public String nameToString (){
			return this.getCourseName() +  " " +  "(" + this.getID() + ")"; 
		}
		
		
		public String toString(boolean isHeader)
		{
			String courseName;
			courseName =  this.nameToString();
			if (isHeader) {
				return "<h2 id=\"course_toString\">" + courseName + "</h2>";
			}
			else {
				if (courseName.length() >=70)
					return  "<a href = \"CourseGroupPage.jsp?courseID=" + this.getID() + "\" id=\"course_toString\"><img src =\"untitled2.bmp\"/></a> <a rel=\"htmltooltip\" >"  + courseName.substring(0,70) + "</a>"
					+ "<div class=\"htmltooltip\">" + courseName + "</div>";
				else
					return  "<a href = \"CourseGroupPage.jsp?courseID=" + this.getID() + "\" id=\"course_toString\"><img src =\"untitled2.bmp\"/></a> <a rel=\"htmltooltip\" >"  + courseName + "</a>"
					+ "<div class=\"htmltooltip\">" + courseName + "</div>";
				
			}
				
		}
		
		public void addLesson(Lesson lesson)
		{
			lessonTimes.add(lesson);
		}

		public String toString(String id,boolean isHeader)	{
		String courseName;
		courseName =  this.nameToString();
		String output;

		if (isHeader) {
			output =  "<h2 id=\"course_toString\">" + courseName + "</h2>";
		}	
		else {
		//	output = "<table id=\"course_toString\"><tr>";
			output = "";
			if (courseName.length() >=70){
				output +=  "<a href = \"CourseGroupPage.jsp?courseID=" + this.getID() + "&FBid=" + id +"\" id=\"course_toString\"><td><img src =\"Classroom.png\"/></td></a><td rel=\"htmltooltip\" >"  + courseName.substring(0,70) + "</td><div class=\"htmltooltip\">" + courseName + "</div>";
			}
			else{
				output += "<a href = \"CourseGroupPage.jsp?courseID=" + this.getID() + "&FBid=" + id +"\" id=\"course_toString\"><td><img src =\"Classroom.png\"/></td></a> <td rel=\"htmltooltip\" >"  + courseName + "</td><div class=\"htmltooltip\">" + courseName + "</div>";
			}
		//	output += "</tr></table";
		}
		return output;
	//	return "<a href = \"CourseGroupPage.jsp?courseID=" + this.getID() + "\" id=\"course_toString\"><td><img src =\"Classroom.png\"/></td></a><td rel=\"htmltooltip\" >"  + courseName + "</td>"+ "<div class=\"htmltooltip\">" + courseName + "</div></tr></table";
	}

}